
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>php-mysql · AGou's StudyNote</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="AGou">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-lightbox/css/lightbox.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../MongoDB.html" />
    
    
    <link rel="prev" href="MySQL、MariaDB基础.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"repo":"AGou-ops/myStudyNote","types":["star","watch","fork"],"size":"small"};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://agou-ops.github.io" target="_blank" class="custom-link">◆点击进入我的个人博客</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                        <b>1.1.</b>
                    
                    关于我
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../Program_lang/">
            
                <a href="../../Program_lang/">
            
                    
                        <b>1.2.</b>
                    
                    编程语言
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../Program_lang/Python.html">
            
                <a href="../../Program_lang/Python.html">
            
                    
                        <b>1.2.1.</b>
                    
                    Python
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../Program_lang/Java.html">
            
                <a href="../../Program_lang/Java.html">
            
                    
                        <b>1.2.2.</b>
                    
                    Java
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../Program_lang/Splash_Lua.html">
            
                <a href="../../Program_lang/Splash_Lua.html">
            
                    
                        <b>1.2.3.</b>
                    
                    Splash_Lua
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../">
            
                <a href="../">
            
                    
                        <b>1.3.</b>
                    
                    数据库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="README.md">
            
                <span>
            
                    
                        <b>1.3.1.</b>
                    
                    Mysql/MariaDB
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1.1" data-path="MySQL、MariaDB基础.html">
            
                <a href="MySQL、MariaDB基础.html">
            
                    
                        <b>1.3.1.1.</b>
                    
                    MySQL/MariaDB基础
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.1.2" data-path="php-mysql.html">
            
                <a href="php-mysql.html">
            
                    
                        <b>1.3.1.2.</b>
                    
                    php-mysql
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../MongoDB.html">
            
                <a href="../MongoDB.html">
            
                    
                        <b>1.3.2.</b>
                    
                    MongoDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../Redis.html">
            
                <a href="../Redis.html">
            
                    
                        <b>1.3.3.</b>
                    
                    Redis
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../Linux/">
            
                <a href="../../Linux/">
            
                    
                        <b>1.4.</b>
                    
                    Linux
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../Linux/Linux基础.html">
            
                <a href="../../Linux/Linux基础.html">
            
                    
                        <b>1.4.1.</b>
                    
                    Linux基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../Linux/Linux启动流程、内核、grub、模块、内核的编译、anaconda自动化.html">
            
                <a href="../../Linux/Linux启动流程、内核、grub、模块、内核的编译、anaconda自动化.html">
            
                    
                        <b>1.4.2.</b>
                    
                    Linux启动流程、内核、grub、模块等
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../Linux/Linux服务器.html">
            
                <a href="../../Linux/Linux服务器.html">
            
                    
                        <b>1.4.3.</b>
                    
                    Linux服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../../Linux/Linux状态信息.html">
            
                <a href="../../Linux/Linux状态信息.html">
            
                    
                        <b>1.4.4.</b>
                    
                    Linux状态信息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../../Linux/Linux网络客户端工具.html">
            
                <a href="../../Linux/Linux网络客户端工具.html">
            
                    
                        <b>1.4.5.</b>
                    
                    Linux网络客户端工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../../Linux/Linux网络配置.html">
            
                <a href="../../Linux/Linux网络配置.html">
            
                    
                        <b>1.4.6.</b>
                    
                    Linux网络配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../../Linux/Linux配置文件.md">
            
                <span>
            
                    
                        <b>1.4.7.</b>
                    
                    Linux配置文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../../Linux/Linux时间服务器.html">
            
                <a href="../../Linux/Linux时间服务器.html">
            
                    
                        <b>1.4.8.</b>
                    
                    Linux时间服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../../Linux/Linux日志管理系统.html">
            
                <a href="../../Linux/Linux日志管理系统.html">
            
                    
                        <b>1.4.9.</b>
                    
                    Linux日志管理系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../../Linux/sudo详解.html">
            
                <a href="../../Linux/sudo详解.html">
            
                    
                        <b>1.4.10.</b>
                    
                    sudo详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.11" data-path="../../Linux/SElinux.html">
            
                <a href="../../Linux/SElinux.html">
            
                    
                        <b>1.4.11.</b>
                    
                    SELinux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.12" data-path="../../Linux/iptables.html">
            
                <a href="../../Linux/iptables.html">
            
                    
                        <b>1.4.12.</b>
                    
                    iptables
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.13" data-path="../../Linux/Linux_shell.html">
            
                <a href="../../Linux/Linux_shell.html">
            
                    
                        <b>1.4.13.</b>
                    
                    Linux Shell
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.14" data-path="../../Linux/shell_awk.html">
            
                <a href="../../Linux/shell_awk.html">
            
                    
                        <b>1.4.14.</b>
                    
                    shell_awk
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.15" data-path="../../Linux/OpenSSL、PKI搭建、算法.html">
            
                <a href="../../Linux/OpenSSL、PKI搭建、算法.html">
            
                    
                        <b>1.4.15.</b>
                    
                    OpenSSL、PKI搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.16" data-path="../../Linux/DNS服务器.html">
            
                <a href="../../Linux/DNS服务器.html">
            
                    
                        <b>1.4.16.</b>
                    
                    DNS服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.17" data-path="../../Linux/Frp内网穿透.html">
            
                <a href="../../Linux/Frp内网穿透.html">
            
                    
                        <b>1.4.17.</b>
                    
                    Frp内网穿透
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.18" data-path="../../Linux/Linux其他.md">
            
                <span>
            
                    
                        <b>1.4.18.</b>
                    
                    Linux其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../Linux_FileSystem/">
            
                <a href="../../Linux_FileSystem/">
            
                    
                        <b>1.5.</b>
                    
                    Linux文件系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../../Linux_FileSystem/FTP.html">
            
                <a href="../../Linux_FileSystem/FTP.html">
            
                    
                        <b>1.5.1.</b>
                    
                    FTP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../../Linux_FileSystem/SAMBA.html">
            
                <a href="../../Linux_FileSystem/SAMBA.html">
            
                    
                        <b>1.5.2.</b>
                    
                    SAMBA
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../../Linux_Tools/">
            
                <a href="../../Linux_Tools/">
            
                    
                        <b>1.6.</b>
                    
                    Linux工具
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../Linux_Tools/fzf工具.html">
            
                <a href="../../Linux_Tools/fzf工具.html">
            
                    
                        <b>1.6.1.</b>
                    
                    fzf工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../../Linux_Tools/工具集合.html">
            
                <a href="../../Linux_Tools/工具集合.html">
            
                    
                        <b>1.6.2.</b>
                    
                    Linux工具集合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../../WEB_Server/">
            
                <a href="../../WEB_Server/">
            
                    
                        <b>1.7.</b>
                    
                    网站服务器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../../WEB_Server/HTML基础.html">
            
                <a href="../../WEB_Server/HTML基础.html">
            
                    
                        <b>1.7.1.</b>
                    
                    HTML基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../../WEB_Server/WEB基础.html">
            
                <a href="../../WEB_Server/WEB基础.html">
            
                    
                        <b>1.7.2.</b>
                    
                    WEB基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../../WEB_Server/httpd.html">
            
                <a href="../../WEB_Server/httpd.html">
            
                    
                        <b>1.7.3.</b>
                    
                    httpd/Apache
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../../WEB_Server/httpd.conf文件详解.html">
            
                <a href="../../WEB_Server/httpd.conf文件详解.html">
            
                    
                        <b>1.7.4.</b>
                    
                    httpd.conf文件详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../../WEB_Server/Nginx.html">
            
                <a href="../../WEB_Server/Nginx.html">
            
                    
                        <b>1.7.5.</b>
                    
                    Nginx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../../WEB_Server/HTTP状态码-详情.html">
            
                <a href="../../WEB_Server/HTTP状态码-详情.html">
            
                    
                        <b>1.7.6.</b>
                    
                    HTTP状态码-详情
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../../Common_Framework/">
            
                <a href="../../Common_Framework/">
            
                    
                        <b>1.8.</b>
                    
                    常用框架
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../../Common_Framework/LNMP.html">
            
                <a href="../../Common_Framework/LNMP.html">
            
                    
                        <b>1.8.1.</b>
                    
                    LNMP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../../Common_Framework/LAMP.html">
            
                <a href="../../Common_Framework/LAMP.html">
            
                    
                        <b>1.8.2.</b>
                    
                    LAMP
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../../Docker&K8s/">
            
                <a href="../../Docker&K8s/">
            
                    
                        <b>1.9.</b>
                    
                    容器及容器编排
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../../Docker&K8s/Docker/Docker.html">
            
                <a href="../../Docker&K8s/Docker/Docker.html">
            
                    
                        <b>1.9.1.</b>
                    
                    Docker
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1.1" data-path="../../Docker&K8s/Docker/Docker.html">
            
                <a href="../../Docker&K8s/Docker/Docker.html">
            
                    
                        <b>1.9.1.1.</b>
                    
                    Docker
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../../Docker&K8s/K8s/">
            
                <a href="../../Docker&K8s/K8s/">
            
                    
                        <b>1.9.2.</b>
                    
                    K8s
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.2.1" data-path="../../Docker&K8s/K8s/K8s基础知识.html">
            
                <a href="../../Docker&K8s/K8s/K8s基础知识.html">
            
                    
                        <b>1.9.2.1.</b>
                    
                    K8s基础知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2.2" data-path="../../Docker&K8s/K8s/K8s安装与部署.html">
            
                <a href="../../Docker&K8s/K8s/K8s安装与部署.html">
            
                    
                        <b>1.9.2.2.</b>
                    
                    K8s安装与部署
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../../Git/">
            
                <a href="../../Git/">
            
                    
                        <b>1.10.</b>
                    
                    Git
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../../Git/Git基础.html">
            
                <a href="../../Git/Git基础.html">
            
                    
                        <b>1.10.1.</b>
                    
                    Git基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../../Git/GitLab服务器.html">
            
                <a href="../../Git/GitLab服务器.html">
            
                    
                        <b>1.10.2.</b>
                    
                    GitLab服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../../Windows/">
            
                <a href="../../Windows/">
            
                    
                        <b>1.11.</b>
                    
                    Windows系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../../Windows/cmd命令.html">
            
                <a href="../../Windows/cmd命令.html">
            
                    
                        <b>1.11.1.</b>
                    
                    cmd命令
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../../Vim.html">
            
                <a href="../../Vim.html">
            
                    
                        <b>1.12.</b>
                    
                    Vim
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../../i3WM快捷键.html">
            
                <a href="../../i3WM快捷键.html">
            
                    
                        <b>1.13.</b>
                    
                    i3WM快捷键
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../../ADB命令.html">
            
                <a href="../../ADB命令.html">
            
                    
                        <b>1.14.</b>
                    
                    ADB命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../../Tmux.html">
            
                <a href="../../Tmux.html">
            
                    
                        <b>1.15.</b>
                    
                    Tmux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../../Kindle.html">
            
                <a href="../../Kindle.html">
            
                    
                        <b>1.16.</b>
                    
                    Kindle越狱指南
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../../Other/">
            
                <a href="../../Other/">
            
                    
                        <b>1.17.</b>
                    
                    Other
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.17.1" data-path="../../Other/pandoc.html">
            
                <a href="../../Other/pandoc.html">
            
                    
                        <b>1.17.1.</b>
                    
                    pandoc
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.2" data-path="../../Other/jupyter&reveal.js.html">
            
                <a href="../../Other/jupyter&reveal.js.html">
            
                    
                        <b>1.17.2.</b>
                    
                    jupyter & reveal.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.3" data-path="../../Other/json.html">
            
                <a href="../../Other/json.html">
            
                    
                        <b>1.17.3.</b>
                    
                    json
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.4" data-path="../../Other/Reveal_js.html">
            
                <a href="../../Other/Reveal_js.html">
            
                    
                        <b>1.17.4.</b>
                    
                    Reveal_js
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >php-mysql</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-anchor"></i><ul><li><a href="#mysqli">1. MySQLi</a></li><ul><li><a href="#mysqli&#x7684;&#x5B89;&#x88C5;">1.1. MySQLi&#x7684;&#x5B89;&#x88C5;</a></li><li><a href="#php&#x8FDE;&#x63A5;mysqli">1.2. php&#x8FDE;&#x63A5;Mysqli</a></li><li><a href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;">1.3. &#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;</a></li><li><a href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;">1.4. &#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;</a></li><li><a href="#&#x63D2;&#x5165;&#x6570;&#x636E;">1.5. &#x63D2;&#x5165;&#x6570;&#x636E;</a></li></ul><li><a href="#pdo">2. PDO</a></li><ul><li><a href="#pdo&#x7684;&#x5B89;&#x88C5;">2.1. PDO&#x7684;&#x5B89;&#x88C5;</a></li><li><a href="#php&#x8FDE;&#x63A5;pdo">2.2. php&#x8FDE;&#x63A5;PDO</a></li><li><a href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;">2.3. &#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;</a></li><li><a href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;">2.4. &#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;</a></li><li><a href="#&#x63D2;&#x5165;&#x6570;&#x636E;">2.5. &#x63D2;&#x5165;&#x6570;&#x636E;</a></li></ul></ul></div><a href="#mysqli" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><blockquote>
<p>PHP 5 &#x53CA;&#x4EE5;&#x4E0A;&#x7248;&#x672C;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x65B9;&#x5F0F;&#x8FDE;&#x63A5; MySQL :</p>
<ul>
<li><code>MySQLi extension</code> &#xFF08;&quot;i&quot; &#x610F;&#x4E3A; improved&#xFF09;</li>
<li><a href="https://baike.baidu.com/item/PDO" target="_blank"><code>PDO (PHP Data Objects)</code></a>&#xFF08;&#x652F;&#x6301;&#x591A;&#x79CD;&#x6570;&#x636E;&#x5E93;&#xFF09;</li>
</ul>
</blockquote>
<h1 id="mysqli"><a name="mysqli" class="anchor-navigation-ex-anchor" href="#mysqli"><i class="fa fa-link" aria-hidden="true"></i></a>1. MySQLi</h1>
<h2 id="mysqli&#x7684;&#x5B89;&#x88C5;"><a name="mysqli&#x7684;&#x5B89;&#x88C5;" class="anchor-navigation-ex-anchor" href="#mysqli&#x7684;&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. MySQLi&#x7684;&#x5B89;&#x88C5;</h2>
<p>&#x5B89;&#x88C5;&#x5B8C;<code>php-mysql</code>&#x5305;&#x65F6;&#x4F1A;&#x9ED8;&#x8BA4;&#x5B89;&#x88C5;<code>mysqli</code>&#x6269;&#x5C55;&#xFF0C;&#x56E0;&#x6B64;&#x786E;&#x4FDD;&#x7CFB;&#x7EDF;&#x5B89;&#x88C5;&#x597D;&#x8BE5;&#x5305;&#x5373;&#x53EF;</p>
<pre class="language-"><code class="lang-bash">yum install- y php-mysql
</code></pre>
<h2 id="php&#x8FDE;&#x63A5;mysqli"><a name="php&#x8FDE;&#x63A5;mysqli" class="anchor-navigation-ex-anchor" href="#php&#x8FDE;&#x63A5;mysqli"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. php&#x8FDE;&#x63A5;Mysqli</h2>
<pre class="language-"><code class="lang-php"><span class="token delimiter important">&lt;?php</span>
<span class="token variable">$servername</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;localhost&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$username</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;username&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;password&quot;</span><span class="token punctuation">;</span>

<span class="token comment">// &#x521B;&#x5EFA;&#x8FDE;&#x63A5;</span>
<span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token function">mysqli_connect</span><span class="token punctuation">(</span><span class="token variable">$servername</span><span class="token punctuation">,</span> <span class="token variable">$username</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// &#x68C0;&#x6D4B;&#x8FDE;&#x63A5;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$conn</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;Connection failed: &quot;</span> <span class="token punctuation">.</span> <span class="token function">mysqli_connect_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;&#x8FDE;&#x63A5;&#x6210;&#x529F;&quot;</span><span class="token punctuation">;</span>
<span class="token operator">?</span><span class="token operator">&gt;</span>
</code></pre>
<h2 id="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;"><a name="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. &#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;</h2>
<pre class="language-"><code class="lang-php"><span class="token delimiter important">&lt;?php</span>
<span class="token variable">$servername</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;localhost&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$username</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;username&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;password&quot;</span><span class="token punctuation">;</span>

<span class="token comment">// &#x521B;&#x5EFA;&#x8FDE;&#x63A5;</span>
<span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token function">mysqli_connect</span><span class="token punctuation">(</span><span class="token variable">$servername</span><span class="token punctuation">,</span> <span class="token variable">$username</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// &#x68C0;&#x6D4B;&#x8FDE;&#x63A5;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$conn</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;&#x8FDE;&#x63A5;&#x5931;&#x8D25;: &quot;</span> <span class="token punctuation">.</span> <span class="token function">mysqli_connect_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;</span>
<span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;CREATE DATABASE myDB&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">mysqli_query</span><span class="token punctuation">(</span><span class="token variable">$conn</span><span class="token punctuation">,</span> <span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;&#x6570;&#x636E;&#x5E93;&#x521B;&#x5EFA;&#x6210;&#x529F;&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;Error creating database: &quot;</span> <span class="token punctuation">.</span> <span class="token function">mysqli_error</span><span class="token punctuation">(</span><span class="token variable">$conn</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">mysqli_close</span><span class="token punctuation">(</span><span class="token variable">$conn</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">?</span><span class="token operator">&gt;</span>
</code></pre>
<h2 id="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;"><a name="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4. &#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;</h2>
<pre class="language-"><code class="lang-php"><span class="token delimiter important">&lt;?php</span>
<span class="token variable">$servername</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;localhost&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$username</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;username&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;password&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$dbname</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;myDB&quot;</span><span class="token punctuation">;</span>

<span class="token comment">// &#x521B;&#x5EFA;&#x8FDE;&#x63A5;</span>
<span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token function">mysqli_connect</span><span class="token punctuation">(</span><span class="token variable">$servername</span><span class="token punctuation">,</span> <span class="token variable">$username</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">,</span> <span class="token variable">$dbname</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// &#x68C0;&#x6D4B;&#x8FDE;&#x63A5;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$conn</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;&#x8FDE;&#x63A5;&#x5931;&#x8D25;: &quot;</span> <span class="token punctuation">.</span> <span class="token function">mysqli_connect_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x4F7F;&#x7528; sql &#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;</span>
<span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">mysqli_query</span><span class="token punctuation">(</span><span class="token variable">$conn</span><span class="token punctuation">,</span> <span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;&#x6570;&#x636E;&#x8868; MyGuests &#x521B;&#x5EFA;&#x6210;&#x529F;&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;&#x9519;&#x8BEF;: &quot;</span> <span class="token punctuation">.</span> <span class="token function">mysqli_error</span><span class="token punctuation">(</span><span class="token variable">$conn</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">mysqli_close</span><span class="token punctuation">(</span><span class="token variable">$conn</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">?</span><span class="token operator">&gt;</span>
</code></pre>
<h2 id="&#x63D2;&#x5165;&#x6570;&#x636E;"><a name="&#x63D2;&#x5165;&#x6570;&#x636E;" class="anchor-navigation-ex-anchor" href="#&#x63D2;&#x5165;&#x6570;&#x636E;"><i class="fa fa-link" aria-hidden="true"></i></a>1.5. &#x63D2;&#x5165;&#x6570;&#x636E;</h2>
<pre class="language-"><code class="lang-php"><span class="token delimiter important">&lt;?php</span>
<span class="token variable">$servername</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;localhost&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$username</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;username&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;password&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$dbname</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;myDB&quot;</span><span class="token punctuation">;</span>

<span class="token comment">// &#x521B;&#x5EFA;&#x94FE;&#x63A5;</span>
<span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token function">mysqli_connect</span><span class="token punctuation">(</span><span class="token variable">$servername</span><span class="token punctuation">,</span> <span class="token variable">$username</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">,</span> <span class="token variable">$dbname</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// &#x68C0;&#x67E5;&#x94FE;&#x63A5;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$conn</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;&#x8FDE;&#x63A5;&#x5931;&#x8D25;: &quot;</span> <span class="token punctuation">.</span> <span class="token function">mysqli_connect_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&apos;John&apos;, &apos;Doe&apos;, &apos;john@example.com&apos;);&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$sql</span> <span class="token punctuation">.</span><span class="token operator">=</span> <span class="token double-quoted-string string">&quot;INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&apos;Mary&apos;, &apos;Moe&apos;, &apos;mary@example.com&apos;);&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$sql</span> <span class="token punctuation">.</span><span class="token operator">=</span> <span class="token double-quoted-string string">&quot;INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&apos;Julie&apos;, &apos;Dooley&apos;, &apos;julie@example.com&apos;)&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">mysqli_multi_query</span><span class="token punctuation">(</span><span class="token variable">$conn</span><span class="token punctuation">,</span> <span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;&#x65B0;&#x8BB0;&#x5F55;&#x63D2;&#x5165;&#x6210;&#x529F;&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;Error: &quot;</span> <span class="token punctuation">.</span> <span class="token variable">$sql</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">&quot;&lt;br&gt;&quot;</span> <span class="token punctuation">.</span> <span class="token function">mysqli_error</span><span class="token punctuation">(</span><span class="token variable">$conn</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">mysqli_close</span><span class="token punctuation">(</span><span class="token variable">$conn</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">?</span><span class="token operator">&gt;</span>
</code></pre>
<h1 id="pdo"><a name="pdo" class="anchor-navigation-ex-anchor" href="#pdo"><i class="fa fa-link" aria-hidden="true"></i></a>2. PDO</h1>
<h2 id="pdo&#x7684;&#x5B89;&#x88C5;"><a name="pdo&#x7684;&#x5B89;&#x88C5;" class="anchor-navigation-ex-anchor" href="#pdo&#x7684;&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1. PDO&#x7684;&#x5B89;&#x88C5;</h2>
<p>&#x7F16;&#x8BD1;&#x5305;&#x8FDB;&#x884C;&#x5B89;&#x88C5;&#xFF1A;</p>
<p>&#x53C2;&#x8003;&#xFF1A;<a href="https://www.php.net/manual/en/pdo.installation.php#114683" target="_blank">https://www.php.net/manual/en/pdo.installation.php#114683</a></p>
<p>&#x4F7F;&#x7528;yum&#x4ED3;&#x5E93;&#x8FDB;&#x884C;&#x5B89;&#x88C5;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">yum <span class="token function">install</span> php-pdo
</code></pre>
<h2 id="php&#x8FDE;&#x63A5;pdo"><a name="php&#x8FDE;&#x63A5;pdo" class="anchor-navigation-ex-anchor" href="#php&#x8FDE;&#x63A5;pdo"><i class="fa fa-link" aria-hidden="true"></i></a>2.2. php&#x8FDE;&#x63A5;PDO</h2>
<pre class="language-"><code class="lang-php"><span class="token delimiter important">&lt;?php</span>
<span class="token variable">$servername</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;localhost&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$username</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;username&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;password&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PDO</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;mysql:host=<span class="token interpolation"><span class="token variable">$servername</span></span>;&quot;</span><span class="token punctuation">,</span> <span class="token variable">$username</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;&#x8FDE;&#x63A5;&#x6210;&#x529F;&quot;</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
<span class="token keyword">catch</span><span class="token punctuation">(</span>PDOException <span class="token variable">$e</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token variable">$e</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token operator">?</span><span class="token operator">&gt;</span>
</code></pre>
<h2 id="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;"><a name="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a>2.3. &#x521B;&#x5EFA;&#x6570;&#x636E;&#x5E93;</h2>
<pre class="language-"><code class="lang-php"><span class="token delimiter important">&lt;?php</span>
<span class="token variable">$servername</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;localhost&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$username</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;username&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;password&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PDO</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;mysql:host=<span class="token interpolation"><span class="token variable">$servername</span></span>&quot;</span><span class="token punctuation">,</span> <span class="token variable">$username</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x8BBE;&#x7F6E; PDO &#x9519;&#x8BEF;&#x6A21;&#x5F0F;&#x4E3A;&#x5F02;&#x5E38;</span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token constant">PDO</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token constant">ATTR_ERRMODE</span><span class="token punctuation">,</span> <span class="token constant">PDO</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token constant">ERRMODE_EXCEPTION</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;CREATE DATABASE myDBPDO&quot;</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x4F7F;&#x7528; exec() &#xFF0C;&#x56E0;&#x4E3A;&#x6CA1;&#x6709;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;</span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;&#x6570;&#x636E;&#x5E93;&#x521B;&#x5EFA;&#x6210;&#x529F;&lt;br&gt;&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span><span class="token punctuation">(</span>PDOException <span class="token variable">$e</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token variable">$sql</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">&quot;&lt;br&gt;&quot;</span> <span class="token punctuation">.</span> <span class="token variable">$e</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">;</span>
<span class="token operator">?</span><span class="token operator">&gt;</span>
</code></pre>
<h2 id="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;"><a name="&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;"><i class="fa fa-link" aria-hidden="true"></i></a>2.4. &#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;</h2>
<pre class="language-"><code class="lang-php"><span class="token delimiter important">&lt;?php</span>
<span class="token variable">$servername</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;localhost&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$username</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;username&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;password&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$dbname</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;myDBPDO&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PDO</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;mysql:host=<span class="token interpolation"><span class="token variable">$servername</span></span>;dbname=<span class="token interpolation"><span class="token variable">$dbname</span></span>&quot;</span><span class="token punctuation">,</span> <span class="token variable">$username</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x8BBE;&#x7F6E; PDO &#x9519;&#x8BEF;&#x6A21;&#x5F0F;&#xFF0C;&#x7528;&#x4E8E;&#x629B;&#x51FA;&#x5F02;&#x5E38;</span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token constant">PDO</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token constant">ATTR_ERRMODE</span><span class="token punctuation">,</span> <span class="token constant">PDO</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token constant">ERRMODE_EXCEPTION</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x4F7F;&#x7528; sql &#x521B;&#x5EFA;&#x6570;&#x636E;&#x8868;</span>
    <span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;CREATE TABLE MyGuests (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP
    )&quot;</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x4F7F;&#x7528; exec() &#xFF0C;&#x6CA1;&#x6709;&#x7ED3;&#x679C;&#x8FD4;&#x56DE; </span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;&#x6570;&#x636E;&#x8868; MyGuests &#x521B;&#x5EFA;&#x6210;&#x529F;&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span><span class="token punctuation">(</span>PDOException <span class="token variable">$e</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token variable">$sql</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">&quot;&lt;br&gt;&quot;</span> <span class="token punctuation">.</span> <span class="token variable">$e</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">;</span>
<span class="token operator">?</span><span class="token operator">&gt;</span>
</code></pre>
<h2 id="&#x63D2;&#x5165;&#x6570;&#x636E;"><a name="&#x63D2;&#x5165;&#x6570;&#x636E;" class="anchor-navigation-ex-anchor" href="#&#x63D2;&#x5165;&#x6570;&#x636E;"><i class="fa fa-link" aria-hidden="true"></i></a>2.5. &#x63D2;&#x5165;&#x6570;&#x636E;</h2>
<pre class="language-"><code class="lang-php"><span class="token delimiter important">&lt;?php</span>
<span class="token variable">$servername</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;localhost&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$username</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;username&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$password</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;password&quot;</span><span class="token punctuation">;</span>
<span class="token variable">$dbname</span> <span class="token operator">=</span> <span class="token double-quoted-string string">&quot;myDBPDO&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PDO</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;mysql:host=<span class="token interpolation"><span class="token variable">$servername</span></span>;dbname=<span class="token interpolation"><span class="token variable">$dbname</span></span>&quot;</span><span class="token punctuation">,</span> <span class="token variable">$username</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// set the PDO error mode to exception</span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token constant">PDO</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token constant">ATTR_ERRMODE</span><span class="token punctuation">,</span> <span class="token constant">PDO</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token constant">ERRMODE_EXCEPTION</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x5F00;&#x59CB;&#x4E8B;&#x52A1;</span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">beginTransaction</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// SQL &#x8BED;&#x53E5;</span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;INSERT INTO MyGuests (firstname, lastname, email) 
    VALUES (&apos;John&apos;, &apos;Doe&apos;, &apos;john@example.com&apos;)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;INSERT INTO MyGuests (firstname, lastname, email) 
    VALUES (&apos;Mary&apos;, &apos;Moe&apos;, &apos;mary@example.com&apos;)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token double-quoted-string string">&quot;INSERT INTO MyGuests (firstname, lastname, email) 
    VALUES (&apos;Julie&apos;, &apos;Dooley&apos;, &apos;julie@example.com&apos;)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x63D0;&#x4EA4;&#x4E8B;&#x52A1;</span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">echo</span> <span class="token double-quoted-string string">&quot;&#x65B0;&#x8BB0;&#x5F55;&#x63D2;&#x5165;&#x6210;&#x529F;&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span><span class="token punctuation">(</span>PDOException <span class="token variable">$e</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">// &#x5982;&#x679C;&#x6267;&#x884C;&#x5931;&#x8D25;&#x56DE;&#x6EDA;</span>
    <span class="token variable">$conn</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">rollback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">echo</span> <span class="token variable">$sql</span> <span class="token punctuation">.</span> <span class="token double-quoted-string string">&quot;&lt;br&gt;&quot;</span> <span class="token punctuation">.</span> <span class="token variable">$e</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token variable">$conn</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">;</span>
<span class="token operator">?</span><span class="token operator">&gt;</span>
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; AGou 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2020-03-02 20:42:24
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="MySQL、MariaDB基础.html" class="navigation navigation-prev " aria-label="Previous page: MySQL/MariaDB基础">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../MongoDB.html" class="navigation navigation-next " aria-label="Next page: MongoDB">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"php-mysql","level":"1.3.1.2","depth":3,"next":{"title":"MongoDB","level":"1.3.2","depth":2,"path":"Database/MongoDB.md","ref":"Database/MongoDB.md","articles":[]},"previous":{"title":"MySQL/MariaDB基础","level":"1.3.1.1","depth":3,"path":"Database/Mysql、MariaDB/MySQL、MariaDB基础.md","ref":"Database/Mysql、MariaDB/MySQL、MariaDB基础.md","articles":[]},"dir":"ltr"},"config":{"plugins":["github@^2.0.0","edit-link@^2.0.2","anchors@^0.7.1","include-codeblock@^3.0.2","tbfed-pagefooter@^0.0.1","expandable-chapters-small@^0.1.7","anchor-navigation-ex@0.1.8","prism","-highlight","insert-logo","-lunr","-search","search-pro","splitter","lightbox","github-buttons","-sharing","sharing-plus","donate","code","-klipse","livereload"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright © AGou 2020","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{"css":["prismjs/themes/prism-tomorrow.css"]},"github":{"url":"https://github.com/AGou-ops"},"livereload":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"code":{"copyButtons":true},"donate":{"alipay":"https://agou-ops.github.io/images/alipay.png","alipayText":"支付宝打赏","button":"打赏","title":"","wechat":"https://agou-ops.github.io/images/wechatpay.png","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"anchor-navigation-ex":{"isRewritePageTitle":true,"tocLevel1Icon":"fa fa-hand-o-right","tocLevel2Icon":"fa fa-hand-o-right","tocLevel3Icon":"fa fa-hand-o-right"},"lightbox":{"jquery":true,"sameUuid":false},"github-buttons":{"repo":"AGou-ops/myStudyNote","types":["star","watch","fork"],"size":"small"},"expandable-chapters-small":{},"include-codeblock":{"check":false,"edit":true,"fixlang":false,"lang":"","template":"ace","theme":"chrome","unindent":true},"sharing":{"qq":true,"all":["douban","facebook","google","hatenaBookmark","instapaper","linkedin","twitter","weibo","messenger","qq","qzone","viber","vk","weibo","pocket","stumbleupon","whatsapp"],"douban":false,"facebook":false,"weibo":true,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":true,"linkedin":false},"edit-link":{"label":"Edit This Page","base":"https://github.com/AGou-ops/myStudyNote"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":true},"anchors":{},"insert-logo":{"style":"background: none; max-height: 50px; min-height: 50px","url":"https://s2.ax1x.com/2019/12/10/QBD0xO.jpg"}},"theme":"default","author":"AGou","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"AGou's StudyNote","language":"zh-hans","output.name":"site","links":{"sidebar":{"◆点击进入我的个人博客":"http://agou-ops.github.io"}},"gitbook":"3.2.3","description":"岂能尽如人意，但求无愧我心。"},"file":{"path":"Database/Mysql、MariaDB/php-mysql.md","mtime":"2020-03-02T12:42:24.280Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-03-02T12:42:32.383Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lightbox/js/lightbox.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

